<?php
/**
 * Created by PhpStorm.
 * User: 24369
 * Date: 2021/6/24
 * Time: 15:00
 */
namespace app\admin\controller;

use app\admin\BaseController;
use app\admin\logic\AdminLogic;
use app\admin\logic\AuthLogic;
use app\admin\logic\GoodLogic;
use app\admin\model\AuthGroup;


use app\admin\model\AuthGroupAccess;
use app\admin\model\AuthRule;
use think\facade\Request;
use think\facade\View;

class Admin extends BaseController
{
    public function auth()
    {
        //权限组展示
        $auth=AuthGroup::field(true)->paginate(5);
        View::assign('auth',$auth);
        return  view();
    }
    //权限添加
    public function authAdd()
    {
        if (Request::isPost()){
            //添加数据验证
            list($msg,$post)=AuthLogic::getAuthData();
            if (isset($msg)&&$msg!='')      $this->error($msg);
            //把权限数组转为字符串
            $post['rules']=implode(',',$post['rules']);
            //添加时间
            $post['addtime']=time();
            //权限组添加
            $res=AuthGroup::create($post);
            if ($res){
                $this->success('添加成功',url('Admin/auth'));
            }else{
                $this->error('添加失败');
            }
        }else{
            //查询权限
            $rule=AuthRule::field(true)->select();
            View::assign('rule',$rule);
            return  view();
        }
    }
    //权限修改
    public function authEdit()
    {
        $id=Request::param('id');
        if (Request::isPost()){
            //验证修改数据
            list($msg,$post)=AuthLogic::getAuthData();
            if (isset($msg)&&$msg!='')      $this->error($msg);
            //把权限组转为字符串
            $post['rules']=implode(',',$post['rules']);
            //修改id
            $post['id']=$id;
            $res=AuthGroup::update($post);
            if ($res){
                $this->success('修改成功',url('Admin/auth'));
            }else{
                $this->error('修改失败');
            }
        }else{
            if ($id){
                //当有修改id时展示该id数据
                $auth=AuthGroup::field(true )->find($id)->toArray();
                $auth['rules']=explode(',',$auth['rules']);
                View::assign($auth);
            }
            //查询权限组
            $rule=AuthRule::field(true)->select();
            View::assign('rule',$rule);
            return  view();
        }
    }
    //权限删除
    public function authSoftDelete()
    {
        $id=Request::param('id');
        if ($id){
            $del=AuthGroup::destroy($id);
            if ($del){
                $this->success('删除成功',url('Admin/auth'));
            }else{
                $this->error('删除失败',url('Admin/auth'));
            }
        }
    }
    //管理员展示
    public function user()
    {
        //管理员展示
        $list=\app\admin\model\Admin::getAdminList();
        View::assign('list',$list);
        return  view();
    }
    //管理员添加
    public function userAdd()
    {
        if (Request::isPost()){
            //添加数据验证
            list($msg,$post)=AdminLogic::getAdminDate('add');
            if (isset($msg)&&$msg!='')      $this->error($msg);
            //图片上传
            list($msg,$path)=GoodLogic::getImg('logo','images');
            if (isset($msg)&&$msg!='')      $this->error($msg);
            if (isset($path)&&$path!='')    $post['headimg']=$path;
            //添加时间
            $post['addtime']=time();
            //密码md5加密
            $post['pwd']=md5($post['pwd']);
            $res=\app\admin\model\Admin::create($post);
            if ($res){
                //新添加的id
                $id=$res->id;
                $post['uid']=$id;
                $auth_group=AuthGroupAccess::create($post);
                if ($auth_group){
                    $this->success('添加成功',url('Admin/user'));
                }else{
                    $this->error('添加失败');
                }
            }else{
                $this->error('添加失败');
            }
        }else{
            //查询权限
            $auth_group=AuthGroup::getAuthGroup();
            View::assign('auth_group',$auth_group);
            return  view();
        }
    }
    //管理员修改
    public function userEdit()
    {
        $id=Request::param('id');
        if (Request::isPost()){
            //修改数据验证
            list($msg,$post)=AdminLogic::getAdminDate('edit');
            if (isset($msg)&&$msg!='')      $this->error($msg);
            //图片上传
            list($msg,$path)=GoodLogic::getImg('logo','images');
            if (isset($msg)&&$msg!='')      $this->error($msg);
            if (isset($path)&&$path!='')    $post['headimg']=$path;
            $post['id']=$id;
            //管理员修改
            $res=\app\admin\model\Admin::update($post);
            if ($res){
                $post['uid']=$id;
                //权限修改
                $auth=AuthGroupAccess::update($post);
                if ($auth){
                    $this->success('修改成功',url('Admin/user'));
                }else{
                    $this->error('修改失败');
                }
            }else{
                $this->error('修改失败');
            }

        }else{
            if ($id){
                //查讯该id的管理员信息
                $user_edit=\app\admin\model\Admin::getAdminEdit($id)->toArray();
                View::assign($user_edit);
            }
            //查询权限
            $group=AuthGroup::field(true)->select();
            View::assign('group',$group);
            return  view();
        }

    }
    //管理员软删除
    public function userSoftDelete()
    {
        $id=Request::param('id');
        if ($id){
            $del=\app\admin\model\Admin::destroy($id);
            if ($del){
                $this->success('删除成功',url('Admin/user'));
            }else{
                $this->error('删除成功',url('Admin/user'));
            }
        }
    }

}